import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import login from '@/views/login.vue'
import postlist from '@/views/postlist'
import undit from '@/views/undit'
import text from '@/views/text'
import axios from '@/views/axios'
import { getToken } from '@/utils/myToken'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path: '/postlist',
    name: 'postlist',
    component: postlist,
      },
      {
        path: '/undit',
    name: 'undit',
    component: undit,
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/axios',
    name: 'axios',
    component: axios
  },
  {
    path: '/text',
    name: 'text',
    component: text
  },
]


const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if(to.meta.use===true){
    if(!getToken()){
      return router.replace('/login')
    }
  }
  next()
})
export default router
